﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace kb_24_the_game
{
    public class Player : ISprite
    {
        #region variabele declaratie

        public string loopDirectie = "stilstaan";
        public Texture2D texture;

        public GraphicsDevice graphics { get; set; }

        public SpriteBatch spriteBatch { get; set; }

        public ContentManager content { get; set; }

        public Vector2 tempCurrentFrame;
        public Vector2 playerPosition;

        public Vector2 position { get; set; }

        private float moveSpeed = 0.5f;
        private Animation playerAnimation = new Animation();
        public bool endRechts = false;
        public bool endLinks = false;
        private KeyboardState keyState;
        private KeyboardState PreviousKeyState;
        private bool eersteKeer = true;
        private bool magPlayerVerderSpringen = true;
        private bool jumping = false, falling = false;
        private int maxhoogte;
        public int grondhoogte = 425;
        public bool doorlopen = true;
        public bool magRechts = true;
        public bool magLinks = true;

        #endregion variabele declaratie

        public Player(ContentManager content, Vector2 startPosition)
        {
            this.content = content;
            playerPosition = startPosition;
            playerAnimation.Initialize(playerPosition, new Vector2(4, 2));
            tempCurrentFrame = Vector2.One;
            Load();
        }

        public void Load()
        {
            texture = content.Load<Texture2D>("ClemensSprite");
            playerAnimation.AnimationImage = texture;
        }

        public void Update(GameTime gameTime)
        {
            keyState = Keyboard.GetState();
            playerAnimation.Active = true;
            PreviousKeyState = keyState;
            playerPosition = playerAnimation.Position;
            if (keyState.IsKeyDown(Keys.Left))
            {
                if (playerPosition.X >= 0 && playerPosition.Y <= 425)
                {
                    if (magLinks)
                    {
                        playerPosition.X -= moveSpeed;
                        tempCurrentFrame.Y = 0;
                        loopDirectie = "links";
                        endLinks = false;
                    }
                }
                else
                {
                    endLinks = true;
                }
            }
            else if (keyState.IsKeyDown(Keys.Right) && doorlopen == true)
            {
                if (playerPosition.X + texture.Width < 770 && playerPosition.Y <= 425)
                {
                    if (magRechts)
                    {
                        playerPosition.X += moveSpeed;
                        loopDirectie = "rechts";
                        endRechts = false;
                    }
                    tempCurrentFrame.Y = 1;
                }
                else
                {
                    endRechts = true;
                }
            }
            else
            {
                playerAnimation.Active = false;
                loopDirectie = "stilstaan";
            }
            maxhoogte = berekenMaxHoogte();
            if (playerPosition.Y < maxhoogte)
            {
                magPlayerVerderSpringen = false;
            }
            if (keyState.IsKeyDown(Keys.Up) && magPlayerVerderSpringen == true)
            {
                Jump();
                jumping = true;
                falling = false;
            }
            else
            {
                jumping = false;
            }
            if (playerPosition.Y < grondhoogte && magPlayerVerderSpringen == false)
            {
                Fall();
                eersteKeer = true;
                jumping = false;
                falling = true;
            }
            else
            {
                falling = false;
            }
            if (jumping == false && falling == false)
            {
                eersteKeer = true;
            }
            if (playerPosition.Y == grondhoogte)
            {
                magPlayerVerderSpringen = true;
            }
            if (playerPosition.Y < grondhoogte && jumping == false && falling == false)
            {
                Fall();
                eersteKeer = true;
                magPlayerVerderSpringen = false;
            }
            tempCurrentFrame.X = playerAnimation.CurrentFrame.X;
            playerAnimation.Position = playerPosition;
            playerAnimation.CurrentFrame = tempCurrentFrame;
            playerAnimation.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            playerAnimation.Draw(spriteBatch);
        }

        private int berekenMaxHoogte()
        {
            if (eersteKeer == true)
            {
                int hoogte = (int)playerPosition.Y - (int)150;
                eersteKeer = false;
                return hoogte;
            }
            else
            {
                return maxhoogte;
            }
        }

        public void Jump()
        {
            playerPosition.Y -= moveSpeed * 4;
            //playerPosition.Y *= 0.99f;
        }

        public void Fall()
        {
            playerPosition.Y += moveSpeed * 4;
            //playerPosition.Y *= 1.1f;
        }

        public void Spring()
        {
            float springHoogte = playerPosition.Y - 0.5f;
            if (playerPosition.Y > springHoogte)
            {
                playerPosition.Y *= 0.9f;
            }
            else
            {
                playerPosition.Y *= 1.1f;
            }
        }
    }
}